package com.example.demo.queue;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * @program: scala_sourcce
 * @description:
 * @author: yangyang
 * @create: 2018-11-16 12:10
 **/
public class FileReaderDemo {
    private static AtomicInteger num = new AtomicInteger(1);
    private static class Produce extends Thread{
        private LinkedBlockingQueue<String> queue;
        private String inputFilePath;

        public Produce(LinkedBlockingQueue<String> queue, String inputFilePath) {
            this.queue = queue;
            this.inputFilePath = inputFilePath;
        }
        @Override
        public void run() {
            try {
                Files.readAllLines(Paths.get(inputFilePath))
                    .stream()
//                    .filter(e -> e.startsWith("Hello"))
                    .forEach(e->queue.offer(e));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static class Consumer extends Thread{
        private LinkedBlockingQueue<String> queue ;
        private String outPutDirectory;
        private static Logger logger = Logger.getLogger("mylogger");

        public Consumer(String outPutDirectory,LinkedBlockingQueue<String> queue) {
            this.queue = queue;
            this.outPutDirectory = outPutDirectory;
        }
        private void handleContent(String e){
            try {
                int lineNumber = num.getAndIncrement();
                final Path filePath = Files.createFile(Paths.get(outPutDirectory + lineNumber + ".txt"));
                String content = "第"+lineNumber+"行: "+ e+" "+ LocalDateTime.now().format(
                    DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.SSS"));

                final BufferedWriter bufferedWriter = Files.newBufferedWriter(filePath);
                bufferedWriter.write(content);
                bufferedWriter.flush();

                logger.info(Thread.currentThread().getName()+": "+content);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        @Override
        public void run() {
            while (!queue.isEmpty()){
                String content = queue.poll();
                handleContent(content);
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        String inputFilePath = "/Users/deck/Downloads/dev.txt";
        String outPutDirectory = "/Users/deck/Desktop/files/fileDemo/";

        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();

        new Produce(queue,inputFilePath).start();
        Thread.sleep(1000);

        new Consumer(outPutDirectory,queue).start();
        new Consumer(outPutDirectory,queue).start();
    }
}
